<?
/**
 * VoiceOne Calls
 *
 * Frontend to provide access to the calls_cdr system in voiceone.
 * @package voiceone
 * @subpackage  calls_cdr
 */
  include ("../admin/config/cfg.php");
  global $asterisk,$cwBasePath,$cwlang,$cwLanguage,$cwUserInfo;

  //----------------------------------------   FUNCTION     -------------------

  function durationBeautify($dur) {
    $seconds=intval($dur)%60;
    $minutes=floor((intval($dur)/60)%60);
    $hours=floor(intval($dur)/3600);

    $seconds=str_pad($seconds, 2, "0", STR_PAD_LEFT);
    $minutes=str_pad($minutes, 2, "0", STR_PAD_LEFT);
    $ret="$minutes:$seconds";
    if ($hours>0) $ret="$hours:".$ret;
    return $ret;
  }

  function dateBeautify($date) {
    $dd=explode(" ",$date);
    $ar=explode("-",$dd[0]);
    return "<strong>".$ar[2]."-".$ar[1]."-".$ar[0]."</strong> [".$dd[1]."]";
  }

  //----------------------------------------- PAGE LOGIC -----------------------

  // if an user isn't joined to an extensions, show the select field of all the clients
  if ($cwUserInfo["exten"]=="") {
    if (isset($_POST["source"])) $_SESSION["source"]=$_POST["source"];
    $ret=$asterisk->getClient(0,0);
    if ($err=$asterisk->getError()) die("Error: $err");
    if (is_array($ret))
    foreach($ret as $v) {
      if ($v["exten"]!="") {  //select only users with an extensions
        $fullname=$v["name"]." ".$v["surname"];
        $name=$v["peer"]["name"];
        $protocol=$v["peer"]["protocol"];
        if ($protocol==SIP_PROTOCOL) $s="SIP/".$name;
        else if ($protocol==IAX_PROTOCOL) $s="IAX/".$name;
        else $s="";
        if ($fullname=="") $fullname==$s;
        if ($_SESSION["source"]==$s) $selected=" selected=\"selected\" "; else $selected="";
        $options.="<option value=\"$s\" $selected>$fullname</option>\n";
      }
    }
    $options="<option value=\"\">[All]</option>\n".$options;
    $select = "          <tr>            \n" ;
    $select.= "            <td class=\"required\" style=''><label for=\"source\">".T_("Entity").":</label></td>\n" ;
    $select.= "            <td><select id=\"source\" name=\"source\">$options</select></td>\n" ;
    $select.= "          </tr>\n" ;
  }
  // else (user is joined to an extensions), show only the user's information
  else {
    //echo "<pre>";
    //print_r($cwUserInfo);
    //echo "</pre>";
    // SIP_PROTOCOL
    if ($cwUserInfo["technology"]==SIP) $_SESSION["source"]="SIP/".$cwUserInfo["exten"];
    // IAX_PROTOCOL
    else if ($cwUserInfo["technology"]==IAX2) $_SESSION["source"]="IAX2/".$cwUserInfo["exten"];
  }

  //set default values
  if (!isset($_SESSION["orderBy"])) $_SESSION["orderBy"]="dateDesc";
  if (!isset($_SESSION["type"])) $_SESSION["type"]="a";
  if (!isset($_SESSION["page"])) $_SESSION["page"]="1";

  // SUBMITTED DATA
  if (isset($_GET["last"])) {$_SESSION["orderBy"]="dateDesc"; $_SESSION["type"]="a"; }
  if (isset($_GET["lastreceived"])) {$_SESSION["orderBy"]="dateDesc"; $_SESSION["type"]="r"; }
  if (isset($_GET["lastcalled"])) {$_SESSION["orderBy"]="dateDesc"; $_SESSION["type"]="d"; }
  if (isset($_GET["nextpage"])) {$_SESSION["page"]++; }
  if (isset($_GET["prevpage"])) {$_SESSION["page"]--; }

  if (count($_POST)>0) {
    if (isset($_POST["source"])) $_SESSION["source"]=$_POST["source"];
    if (isset($_POST["type"])) $_SESSION["type"]=$_POST["type"];
    if (isset($_POST["search"])) $_SESSION["search"]=$_POST["search"];
    if (isset($_POST["from"])) $_SESSION["from"]=$_POST["from"];
    if (isset($_POST["to"])) $_SESSION["to"]=$_POST["to"];
    if (isset($_POST["orderBy"])) $_SESSION["orderBy"]=$_POST["orderBy"];
    $_SESSION["page"]=1; //reset the page number
  }
  $source=$_SESSION["source"];
  $type=$_SESSION["type"];
  $search=$_SESSION["search"];
  $from=$_SESSION["from"];
  $to=$_SESSION["to"];
  $orderBy=$_SESSION["orderBy"];
  $page=$_SESSION["page"];
  $entry=Array('source'=>$source,'type'=>$type,'search'=>$search,'from'=>$from,'to'=>$to,'orderBy'=>$orderBy,'page'=>$page);
  $ret=$asterisk->stats($entry);
  $rowNumber=count($ret)*($page-1);
  if (is_array($ret))
  foreach($ret as $r) {
    if ($r["received"]!="" and $r["dialed"]=="") {$img2=""; $img="<img src=\"".$relBasePath."public/img/incoming.gif\" alt=\"Incoming call\" /> "; }
    else if ($r["dialed"]!="" and $r["received"]=="") {$img2=""; $img="<img src=\"".$relBasePath."public/img/outgoing.gif\" alt=\"Outgoing call\" /> "; }
    else { $img=""; $img2=" <img src=\"".$relBasePath."public/img/call.gif\" alt=\"Call\" /> "; }
    $rowNumber++;
    $txt.="    <tr>\n";
    $txt.="          <td style='color: darkred'>".$rowNumber."</td>\n";
    $txt.="          <td>".dateBeautify($r["calldate"])."</td>\n";
    $txt.="          <td>$img ".$r["received"].$img2.$r["dialed"]."</td>\n";
    $txt.="          <td>".durationBeautify($r["duration"])."</td>\n";
    $txt.="    </tr>\n";
  }

  //--------------------------------------- PAGE HTML --------------------------

  ob_start();
?>
  <div class="SectionHeader">
    <h1><?=sprintf(T_("Calls Detail Report - %s"),$source );?></h1>
    <h3>
      <a href="./?last" class="admin" ><?=T_("Last")?></a>
      <a href="./?lastreceived" class="admin" ><?=T_("Last received")?></a>
      <a href="./?lastcalled" class="admin" ><?=T_("Last called")?></a>
    </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">

      <h2><?=T_("Result - Page")?><?=$page?></h2>
      <p class="blockintro"><?=T_("This is the list of the calls resulting from the search")?></p>
      <table id="stats" cellpadding="0" cellspacing="0">
        <tr>
          <th></th>
          <th>Call Date</th>
          <th>Call </th>
          <th>Duration</th>
        </tr>
        <?=$txt?>
      </table>
      <!-- buttons -->
      <table >
        <tr>
          <td><a href="./?prevpage"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-prev.gif" alt="Previous Page" /></a></td>
          <td style="text-align: right"><a href="./?nextpage"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-next.gif" alt="Next Page" /></a></td>
        </tr>
      </table>

    </form>
  </div>
<?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
        <div class="Sidebar">
          <h1><?=T_("Search Panel")?></h1>

          <div id="basic_form">
            <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">

              <p class="blockintro"><?=T_("Fill all the filter to find your calls")?></p>
              <div class="block" style="background: white">
                <table>
                  <?=$select?>
                  <tr>
                    <td class="required" style=''><label for="type"><?=T_("Type")?>:</label></td>
                    <td>
                      <input type="radio" name="type" value="a" <?if ($_SESSION["type"]=="a") echo "checked=\"checked\"";?>/>
                      <img src="<?=$relBasePath?>public/img/outgoing.gif" alt="Outgoing call" />
                      <img src="<?=$relBasePath?>public/img/incoming.gif" alt="Incoming call" /> All<br/>
                      <input type="radio" name="type" value="r" <?if ($_SESSION["type"]=="r") echo "checked=\"checked\"";?>/>
                      <img src="<?=$relBasePath?>public/img/incoming.gif" alt="Incoming call" /> Received<br/>
                      <input type="radio" name="type" value="d" <?if ($_SESSION["type"]=="d") echo "checked=\"checked\"";?>/>
                      <img src="<?=$relBasePath?>public/img/outgoing.gif" alt="Outgoing call" /> Dialed<br/>
                    </td>
                  </tr>
                  <tr>
                    <td class="required" style=''><label for="search"><?=T_("Search")?>:</label></td>
                    <td><input type="text" size="25" maxlength="20" id="search" name="search" value="<?=$_SESSION["search"]?>" /></td>
                  </tr>
                  <tr>
                    <td class="required" style=''><label for="from"><?=T_("From")?>:</label></td>
                    <td><input type="text" size="15" maxlength="15" id="from" name="from" value="<?=$_SESSION["from"]?>" /></td>
                  </tr>
                  <tr>
                    <td class="required" style=''><label for="to"><?=T_("To")?>:</label></td>
                    <td><input type="text" size="15" maxlength="15" id="to" name="to" value="<?=$_SESSION["to"]?>" /></td>
                  </tr>
                  <tr>
                    <td class="required" style=''><label for="orderBy"><?=T_("Order")?>:</label></td>
                    <td >
                      <strong>Date</strong>:<br>
                        <input type="radio" name="orderBy" value="dateDesc" <?if ($_SESSION["orderBy"]=="dateDesc") echo "checked=\"checked\"";?>/><?=T_("Desc")?>
                        <input type="radio" name="orderBy" value="dateAsc" <?if ($_SESSION["orderBy"]=="dateAsc") echo "checked=\"checked\"";?>/><?=T_("Asc")?>
                    </td>
                  </tr>
                  <tr>
                    <td></td>
                    <td >
                      <strong>Duration</strong>:<br>
                        <input type="radio" name="orderBy" value="durationDesc" <?if ($_SESSION["orderBy"]=="durationDesc") echo "checked=\"checked\"";?>/><?=T_("Desc")?>
                        <input type="radio" name="orderBy" value="durationAsc" <?if ($_SESSION["orderBy"]=="durationAsc") echo "checked=\"checked\"";?>/><?=T_("Asc")?>
                    </td>
                  </tr>
                  <tr>
                    <td></td>
                    <td><input type="image"  src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-search.gif" alt="Search" /></td>
                  </tr>
                </table>
              </div>
            </form>
          </div>

        </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

  $smarty->assign("section", "calls");
  $smarty->assign("subsection", "");
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>
